package cn.refactor.typersample;


import cn.refactor.typer.OnWriteTextChangedListener;
import cn.refactor.typer.TyperEditText;
import ohos.aafwk.ability.fraction.Fraction;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Component;
import ohos.agp.components.ComponentContainer;
import ohos.agp.components.LayoutScatter;
import ohos.eventhandler.EventHandler;
import ohos.eventhandler.EventRunner;


/**
 * Create by andy (https://github.com/andyxialm)
 * Create time: 16/10/31 09:08
 * Description : Sample
 */
public class SampleFraction extends Fraction implements SampleContract.View, Component.ClickedListener {

    private SampleContract.SamplePresenter mPresenter;
    private TyperEditText mTyperEditText;

    public static SampleFraction newInstance() {
        return new SampleFraction();
    }

    @Override
    protected Component onComponentAttached(LayoutScatter scatter, ComponentContainer container, Intent intent) {
        Component root = scatter.parse(ResourceTable.Layout_fragment_sample, container, false);
        root.findComponentById(ResourceTable.Id_btn_restart).setClickedListener(this);
        root.findComponentById(ResourceTable.Id_btn_start).setClickedListener(this);

        root.findComponentById(ResourceTable.Id_btn_stop).setClickedListener(this);
        root.findComponentById(ResourceTable.Id_btn_pause).setClickedListener(this);
        root.findComponentById(ResourceTable.Id_btn_resume).setClickedListener(this);

        mTyperEditText = (TyperEditText) root.findComponentById(ResourceTable.Id_typer_edit_text);
        mTyperEditText.setOnWriteTextChangedListener(new OnWriteTextChangedListener() {
            @Override
            public void onChanged(int index) {

            }

            @Override
            public void onCompleted() {

            }
        });
        return root;
    }

    @Override
    protected void onActive() {
        super.onActive();
        mPresenter.loadTextData();
    }


    @Override
    public void setTextData(String textData) {
        mTyperEditText.setSourceText(textData);
    }

    @Override
    public void startTyper() {
        mTyperEditText.start();
    }

    @Override
    public boolean isActive() {
        return true;
//        return isAdded();
    }

    @Override
    public void setPresenter(SampleContract.SamplePresenter presenter) {
        mPresenter = presenter;
    }

    @Override
    public void onClick(Component v) {
        switch (v.getId()) {
            case ResourceTable.Id_btn_restart:
                restartTyper();
                break;
            case ResourceTable.Id_btn_start:
                startTyper();
                break;
            case ResourceTable.Id_btn_stop:
                stopTyper();
                break;
            case ResourceTable.Id_btn_pause:
                pauseTyper();
                break;
            case ResourceTable.Id_btn_resume:
                resumeTyper();
                break;
        }
    }

    private void restartTyper() {
        mTyperEditText.restart();
    }

    private void stopTyper() {
        mTyperEditText.stop();
    }

    private void resumeTyper() {
        mTyperEditText.resume();
    }

    private void pauseTyper() {
        mTyperEditText.pause();
    }
}
